package cn.snowheart.template;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import java.io.FileWriter;

/**
 * @author liuwanxiang
 * @version 2019/07/08
 */
public class XmlFileWriter extends MyFileWriter {

    /**
     * 文件已存在时是否删除文件，并重新创建
     * 以文件名做参数，可根据文件名做相关的逻辑判断
     *
     * @param fileName
     * @return
     */
    @Override
    protected boolean ifDelete(String fileName) {
        return true;
    }

    @Override
    protected void writeFile(FileWriter fw, Object content) throws Exception {
        JAXBContext context = JAXBContext.newInstance(Entity.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.marshal(content, fw);
        fw.flush();
        fw.close();
    }

    /**
     * 预留钩子，比如说将文件通过FTP传到某个目录神马的
     */
    @Override
    protected void doSomeThing() {
        System.out.println("Notice: xml file write end...");
    }
}
